﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ChGK.Data;
using CHGK.web.UI.Core;

namespace CHGK.web.UI.Models
{
    public class TeamRepositry
    {
        CHGKModelContainer _CHGKContainer;

        public TeamRepositry()
        {
            _CHGKContainer = new CHGKModelContainer();
        }

        public TeamRepositry(CHGKModelContainer CHGKContainer)
        {
            _CHGKContainer = CHGKContainer;
        }

        public IEnumerable<Team> GetTeams(int compId)
        {
            Competition dbCompetition = _CHGKContainer.Competitions.SingleOrDefault(t => t.ID == compId);
           
            if (dbCompetition == null)
            {
                return null;
            }
            else
            {
                return _CHGKContainer.Teams.Where(t => t.CompetitionID == compId).OrderBy(t=>t.Number);
            }
        }

        public IEnumerable<Team> GetTeams()
        {
            return _CHGKContainer.Teams.OrderBy(t=>t.Number);
        }

        public Team GetTeam(int id)
        {
            Team dbTeam = _CHGKContainer.Teams.SingleOrDefault(t => t.ID == id);

            if (dbTeam == null)
            {
                return null;
            }
            else
            {
                return dbTeam;
            }
        }

        public Team GetTeam(int CompId, int NumberInComp)
        {
            Team dbTeam = _CHGKContainer.Teams.SingleOrDefault(t => t.CompetitionID == CompId && t.Number == NumberInComp);
           
            if (dbTeam == null)
            {
                return null;
            }
            else
            {
                return dbTeam;
            }
        }

        public int CreateTeam(string MasterLogin, int competitionId, short number, string caption, string City)
        {
            Team NewTeam = new Team();
            CryptoHelper CryptoHelperTemp = new CryptoHelper();
           
            NewTeam.UserLogin = MasterLogin;
            NewTeam.CompetitionID = competitionId;
            NewTeam.Number = number;
            NewTeam.Caption = caption;
            NewTeam.City = City;
            NewTeam.Disqualified = false;

            _CHGKContainer.Teams.AddObject(NewTeam);
            _CHGKContainer.SaveChanges();

            return NewTeam.ID;
        }


        public void UpdateTeam(Team userTeam)
        {
            Team dbTeam = GetTeam(userTeam.ID);
           
            dbTeam.UserLogin = userTeam.UserLogin;
            dbTeam.CompetitionID = userTeam.CompetitionID;
            dbTeam.Number = userTeam.Number;
            dbTeam.Caption = userTeam.Caption;
            dbTeam.City = userTeam.City;
            dbTeam.Disqualified = userTeam.Disqualified;
            _CHGKContainer.SaveChanges();

        }

        public void UpdateTeam(int TeamID, string TeamCaption, string TeamCity, int TeamNumber)
        {
            Team dbTeam = GetTeam(TeamID);

            dbTeam.Caption = TeamCaption;
            dbTeam.City = TeamCity;
            dbTeam.Number = (short)TeamNumber;

            _CHGKContainer.SaveChanges();

        }
        public void DeleteTeam(int id)
        {
            Team dbTeam = GetTeam(id);
           
            _CHGKContainer.Teams.DeleteObject(dbTeam);
            _CHGKContainer.SaveChanges();

        }

    }
}